package org.linlinjava.litemall.wx.controller;

import io.swagger.annotations.*;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.ResponseUtil.ListResponse;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.wx.annotation.LogAnno;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxIntegralService;
import org.linlinjava.litemall.wx.vo.IntegralExchangeRequest;
import org.linlinjava.litemall.wx.vo.IntegralIndexResponse;
import org.linlinjava.litemall.wx.vo.IntegralListLogResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * wx积分控制器
 * 用户积分服务
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Api(value="用户积分controller",tags={"用户积分操作API"})
@RestController
@RequestMapping("/wx/integral")
@Validated
public class WxIntegralController {

    /**
     * wx积分服务
     */
    @Autowired
    private WxIntegralService wxIntegralService;

    /**
     * 指数
     *
     * @param userId 用户id
     * @return {@link Response}<{@link IntegralIndexResponse}>
     */
    @ApiOperation(value="用户积分信息")
    @GetMapping("index")
    @LogAnno
    public Response index(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        return wxIntegralService.index(userId);
    }

    /**
     * 交换
     *
     * @param userId  用户id
     * @param request 请求
     * @return {@link Response}<{@link Object}>
     */
    @ApiOperation(value="积分兑换")
    @PostMapping("exchange")
    @LogAnno
    public Response<Object> exchange(@LoginUser Integer userId,
    		@ApiParam(name="request",value="积分兑换请求对象",required=true) @Valid @RequestBody IntegralExchangeRequest request) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxIntegralService.exchange(userId, request);
    }

    /**
     * 日志列表
     *
     * @param userId 用户id
     * @param type   类型
     * @param page   页面
     * @param limit  限制
     * @param sort   排序
     * @param order  订单
     * @return {@link Response}<{@link ListResponse}<{@link IntegralListLogResponse}>>
     */
    @ApiOperation(value="积分历史记录查询")
    @GetMapping("list_log")
    @LogAnno
    public Response<ListResponse<IntegralListLogResponse>> listLog(@LoginUser Integer userId,
    		@ApiParam(name="type",value="积分记录类型：0 增加,1 兑换",required=false) Byte type,
	        @ApiParam(name="page",value="分页页数",required=false) @RequestParam(defaultValue = "1") Integer page,
	        @ApiParam(name="limit",value="分页大小",required=false) @RequestParam(defaultValue = "10") Integer limit,
	        @ApiParam(name="sort",value="排序的字段(值为add_time,id)",required=false) @Sort(accepts = {"add_time", "id"}) @RequestParam(defaultValue = "add_time") String sort,
	        @ApiParam(name="order",value="排序的方式(值为desc，asc)",required=false) @Order @RequestParam(defaultValue = "desc") String order) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxIntegralService.ListLog(userId, type, page, limit, sort, order);
    }

    @ApiOperation("积分变更记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "page"),
            @ApiImplicitParam(name = "limit",value = "limit"),
            @ApiImplicitParam(name = "userId",value = "userId")
    })
    @GetMapping("integralChangeLog")
    public Response integralChangeLog(
            @LoginUser Integer userId,
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @RequestParam(name = "limit",defaultValue = "20") Integer limit
    ){
        return wxIntegralService.integralChangeLog(userId,page,limit);
    }


    /**
     * 积分变更记录中的详情
     * @param orderId
     * @return
     */
    @ApiOperation("积分记录详情「增加｜减少」")
    @GetMapping("integralChangeLogDetails")
    public Response integralChangeLogDetails(@RequestParam("orderId") Integer orderId,@ApiIgnore @LoginUser Integer userId){
        return wxIntegralService.integralChangeLogDetails(orderId,userId);
    }

    /**
     * 细节积分
     *
     * @param userId 用户id
     * @param id     id
     * @return {@link Response}
     */
    @ApiOperation("积分详情")
    @GetMapping("detailsIntegral")
    @ApiImplicitParam(name = "id",value = "积分记录ID")
    @LogAnno
    public Response detailsIntegral(@ApiIgnore @LoginUser Integer userId, @RequestParam("id") Integer id){
        return wxIntegralService.detailsIntegral(userId,id);
    }


}
